package com.kaiyiweitong.tongxing.activity.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.constant.HttpConstant;
import com.kaiyiweitong.tongxing.constant.LogicConstant;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.HttpUtil;
import com.kaiyiweitong.tongxing.util.MyTextUtils;
import com.kaiyiweitong.tongxing.util.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/27 0027.
 */

public class ExchangeCouponActivity extends BaseActivity implements TextWatcher {

    @Bind(R.id.et_coupon_num)
    EditText mEtCouponNum;
    @Bind(R.id.iv_pic)
    ImageView mIvPic;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_date)
    TextView mTvDate;
    @Bind(R.id.tv_coupon_value)
    TextView mTvCouponValue;
    @Bind(R.id.tv_coupon_tip)
    TextView mTvCouponTip;
    @Bind(R.id.tv_confirm)
    TextView mTvConfirm;
    @Bind(R.id.ll_coupon_info)
    LinearLayout mLlCouponInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_coupon);
        ButterKnife.bind(this);

        mEtCouponNum.addTextChangedListener(this);

    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        if (MyTextUtils.checkTextContent(mEtCouponNum).equals("")) {
            CommonUtil.toast(this, R.string.exchange_code_not_blank);
        } else {
            searchCoupon();
        }
    }

    private void searchCoupon() {
        Map<String, String> map = new HashMap<>();
        map.put("id", SpUtils.getString(this, LogicConstant.USER_ID));
        map.put("code", MyTextUtils.checkTextContent(mEtCouponNum));
        HttpUtil.get(HttpConstant.EXCHANGE_COUPON, map, this, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                switch (result) {
                    case "\"0\""://兑换失败
                        CommonUtil.toast(ExchangeCouponActivity.this, R.string.exchange_fail);
                        break;
                    case "\"1\""://领取时间已过
                        CommonUtil.toast(ExchangeCouponActivity.this, R.string.exchange_time_out);
                        break;
                    case "\"2\""://已经被领取
                        CommonUtil.toast(ExchangeCouponActivity.this, R.string.had_exchange);
                        break;
                    case "\"3\""://优惠卷失效
                        CommonUtil.toast(ExchangeCouponActivity.this, R.string.coupon_lose_efficacy);
                        break;
                    case "\"4\""://兑换成功
                        CommonUtil.toast(ExchangeCouponActivity.this, R.string.coupon_success);
                        break;
                }
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
